package com.zhangyu.shadow.repair.delegate.dept;

import com.zhangyu.shadow.repair.base.IStringDataConvert;
import com.zhangyu.shadow.repair.config.ErrorMsg;
import com.zhangyu.shadow.repair.delegate.bean.OrgDeptBean;

import org.greenrobot.greendao.annotation.NotNull;

import java.util.List;

import st.lib.base.net.callback.IError;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/5
 */
public class DeptPresenter extends DeptContract.Presenter {

    private final DeptContract.View mView;
    private final DeptContract.Model mModel;

    public DeptPresenter(@NotNull DeptContract.View view) {
        mView = view;
        mModel = new DeptModel();
    }

    @Override
    void getOrgDept() {
        if (mView != null) {
            mView.showLoading("正在获取组织关系...");
        }
        addDisposable(mModel.getOrgDept(new IStringDataConvert<List<OrgDeptBean>>() {
                    @Override
                    public void convert(List<OrgDeptBean> list) {
                        if (list != null) {
                            if (mView != null) {
                                mView.hideLoading();
                                mView.getOrgDeptSucceed(list);
                            }
                        } else {
                            if (mView != null) {
                                mView.getOrgDeptFailed(-1, ErrorMsg.ERROR_DATA);
                            }
                        }
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        if (mView != null) {
                            mView.getOrgDeptFailed(code, msg);
                        }
                    }
                })
        );
    }

    @Override
    void setPersonOrgDept(String personId,
                          String deptId,
                          String groupId) {
        if (mView != null) {
            mView.showLoading("正在保存...");
        }
        addDisposable(mModel.setPersonOrgDept(personId, deptId, groupId
                , new IStringDataConvert<String>() {
                    @Override
                    public void convert(String o) {
                        if (mView != null) {
                            mView.hideLoading();
                            mView.setPersonOrgDeptSucceed();
                        }

                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        if (mView != null) {
                            mView.setPersonOrgDeptFailed(code, msg);
                        }
                    }
                })
        );
    }
}
